<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hook result - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks/poke_exception.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Hook result</h1></div></div></div>
  <p>We now tell Outcome that for every instance of our localised <code>result&lt;T&gt;</code>, that
on failure construction only, we want custom code to be run which increments the current
slot in TLS storage and writes the current stack backtrace into it.</p>

<p>For Outcome before v2.2, we must do this by inserting a specially named free function into
a namespace searched by ADL:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">error_code_extended</span>
<span class="p">{</span>
  <span class="c1">// Specialise the result construction hook for our localised result
</span><span class="c1"></span>  <span class="c1">// We hook any non-copy, non-move, non-inplace construction, capturing a stack backtrace
</span><span class="c1"></span>  <span class="c1">// if the result is errored.
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="p">,</span> <span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">hook_result_construction</span><span class="p">(</span><span class="n">result</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">*</span><span class="n">res</span><span class="p">,</span> <span class="n">U</span> <span class="o">&amp;&amp;</span> <span class="cm">/*unused*/</span><span class="p">)</span> <span class="k">noexcept</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="n">res</span><span class="o">-&gt;</span><span class="n">has_error</span><span class="p">())</span>
    <span class="p">{</span>
      <span class="c1">// Grab the next extended info slot in the TLS
</span><span class="c1"></span>      <span class="n">extended_error_info</span> <span class="o">&amp;</span><span class="n">eei</span> <span class="o">=</span> <span class="n">mythreadlocaldata</span><span class="p">().</span><span class="n">next</span><span class="p">();</span>

      <span class="c1">// Write the index just grabbed into the spare uint16_t
</span><span class="c1"></span>      <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">hooks</span><span class="o">::</span><span class="n">set_spare_storage</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">mythreadlocaldata</span><span class="p">().</span><span class="n">current</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>

      <span class="c1">// Capture a backtrace into my claimed extended info slot in the TLS
</span><span class="c1"></span>      <span class="n">eei</span><span class="p">.</span><span class="n">items</span> <span class="o">=</span> <span class="o">::</span><span class="n">backtrace</span><span class="p">(</span><span class="n">eei</span><span class="p">.</span><span class="n">backtrace</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">eei</span><span class="p">.</span><span class="n">backtrace</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/error_code_extended.cpp#L119" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>For Outcome v2.2 and later, we must do this by using a custom no value policy which contains
a function named <code>on_result_construction()</code>. The function implementation is identical between
both mechanisms, just the name and placement of the function declaration differs.</p>

<p>The only non-obvious part above is the call to <a href="../../../reference/functions/hooks/set_spare_storage.html" class="api-reference"><code>void set_spare_storage(basic_result|basic_outcome *, uint16_t) noexcept</code></a>
.</p>

<p>Both <code>result</code> and <code>outcome</code> keep their internal state metadata in a <code>uint32_t</code>,
half of which is not used by Outcome. As it can be very useful to keep a small
unique number attached to any particular <code>result</code> or <code>outcome</code> instance, we
permit user code to set those sixteen bits to anything they feel like.
The corresponding function to retrieve those sixteen bits is <a href="../../../reference/functions/hooks/spare_storage.html" class="api-reference"><code>uint16_t spare_storage(const basic_result|basic_outcome *) noexcept</code></a>
.</p>

<p>The state of the sixteen bits of spare storage are ignored during comparison operations.</p>

<p>The sixteen bits of spare storage propagate during the following operations:</p>

<ol>
<li>Copy and move construction between <code>result</code>&rsquo;s.</li>
<li>Copy and move construction between <code>outcome</code>&rsquo;s.</li>
<li>Copy and move construction from a <code>result</code> to an <code>outcome</code>.</li>
<li>Converting copy and move constructions for all the above.</li>
<li>Assignment for all of the above.</li>
</ol>

<p>They are NOT propagated in these operations:</p>

<ol>
<li>Any conversion or translation which goes through a <code>failure_type</code> or <code>success_type</code>.</li>
<li>Any conversion or translation which goes through a <code>ValueOrError</code> concept match.</li>
<li>Any unpacking or repacking of value/error/exception e.g. a manual repack of an
<code>outcome</code> into a <code>result</code>.</li>
</ol>


        </div><p><small>Last revised: December 15, 2020 at 12:22:39 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks/poke_exception.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
